using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Formular;

namespace Pixysoft.Weblications.Sango.Battle
{

    class BattleAttackRow : BattleAttack, IBattleAttack
    {

        public BattleAttackRow()
            : base(AttackTypecodeCollection.ATTACK_ROW)
        {
        }

        public void apply(BattleHistory history, Battler attacker, BattleTeam attacks, BattleTeam defenses)
        {
            BattleActionHistory action = history.createAction(attacker, BattleActionTypecodeCollection.NORMAL_ATTACK);

            action.setAttack(this);

            bool availableAttack = false;

            Battler[] availableDefenses = defenses.getRowAppliers(attacker);

            if (availableDefenses.Length <= 0)
                return;

            foreach (Battler defenser in availableDefenses)
            {
                if (!BattleFormular.attackHit(attacker, defenser))
                {
                    action.createItem(defenser, BattleActionItemTypecodeCollection.ATTACK_MISS).Defenser = (defenser.createHistory());

                    continue;
                }

                long hpValue = BattleFormular.attackDamage(attacker, defenser);
                defenser.reduceHpPointValue(hpValue);
                defenser.addPowervalue();

                BattleActionItemHistory attackActionItem = action.createItem(defenser, BattleActionItemTypecodeCollection.ATTACK_SUCCEED);
                attackActionItem.Attacker =attacker.createHistory();
                attackActionItem.Defenser = defenser.createHistory();

                availableAttack = true;
            }

            if (availableAttack)
                attacker.addPowervalue();
        }
    }
}